package com.hxm.demo.kaikeba;

/**
 * @author huangxiaoming
 * @date 2022/6/19
 **/
public class Devide {

    /**
     * 2个n位正整数相乘（n为2的幂次方）
     *
     * @param args
     */
    public static void main(String[] args) {
        int x = 4444;
        int y = 2222;
        System.out.println(devide(x, y));
    }

    public static int devide(int x, int y) {
        String s1 = String.valueOf(x);
        String s2 = String.valueOf(y);
        if (s1.length() == 1 && s2.length() == 1) {
            return x * y;
        }
        int a = Integer.parseInt(s1.substring(0, s1.length() / 2));
        int b = Integer.parseInt(s1.substring(s1.length() / 2));
        int c = Integer.parseInt(s2.substring(0, s2.length() / 2));
        int d = Integer.parseInt(s2.substring(s2.length() / 2));
//        x = a * (int) Math.pow(10, s1.length() / 2) + b;
//        y = c * (int) Math.pow(10, s2.length() / 2) + d;
//        int result = a * c * (int) Math.pow(10, s1.length()) + (a * d + b * c) * (int) Math.pow(10, s1.length() / 2)
//                + b * d;

//        int result = a * c * (int) Math.pow(10, s1.length()) + ((a + b) * (c + d) - a * c - b * d) * (int) Math.pow(10, s1.length() / 2)
//                + b * d;
        return devide(a, c) * (int) Math.pow(10, s1.length()) + (devide(a, d) + devide(b, c)) * (int) Math.pow(10, s1.length() / 2)
                + devide(b, d);
//        int ac = devide(a, c);
//        int bd = devide(b, d);
//        int abcd = devide(a + b, c + d);
//        return ac * (int) Math.pow(10, s1.length()) + (abcd - ac - bd) * (int) Math.pow(10, s1.length() / 2)
//                + bd;
    }
}
